<template>
    <div class="login-page">
        <div class="wrap">
            <div class="loginTitle">蜀丰 OA 后台管理</div>
            <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer" label-width="80px" size="small">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="登录" name="login"></el-tab-pane>
                    <!-- <el-tab-pane label="注册" name="register"></el-tab-pane> -->
                </el-tabs>
                <el-form-item prop="username" label="账号">
                    <el-input type="text" v-model="loginForm.username" placeholder="请输入账号" size="small">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" size="small">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码" v-if="activeName==='register'">
                    <el-input type="password" v-model="loginForm.rePassword" placeholder="请输入二次密码" size="small">
                    </el-input>
                </el-form-item>
                <!-- <el-form-item prop="captcha" label="验证码">
                    <div class="code-wrap">
                        <el-input type="text" v-model="loginForm.captcha" placeholder="请输入验证码" size="small">
                        </el-input>
                        <img v-if="captchaImage" :src="captchaImage" alt="Captcha" fit="contain" @click="getCaptcha" />
                    </div>
                </el-form-item> -->
                <el-button type="primary" style="width: 100%" @click="submitLogin" size="small" :loading="loading">登录</el-button>
            </el-form>
        </div>
    </div>
</template>

<script>
import { getAuthCodeKey, getCaptcha, login } from '@/api/login.js';
import { ElMessage } from 'element-plus'
import { encrypt } from '@/utils/jsencrypt'
import * as Base64 from 'js-base64'
const arrayBufferToBase64 = (buffer) => {
    var binary = ''
    var bytes = new Uint8Array(buffer)
    var len = bytes.byteLength
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i])
    }
    return window.btoa(binary)
}
export default {
    name: "Login",
    data() {
        return {
            loading: false,
            activeName: 'login',
            loginForm: {
                username: "",
                password: "",
                captcha: ""
            },
            checked: true,
            rules: {
                username: [
                    { required: true, message: "请输入用户名", trigger: "blur" },
                ],
                password: [{ required: true, message: "请输入密码", trigger: "blur" }],
                captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
            },
            captchaImage: '',
            secretKey: null
        };
    },
    methods: {
        submitLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    // await this.$api.post("/material/login", this.loginForm);
                    // await this.$getUserInfo()
                    this.loading = true
                    this.confirmLogins()
                } 
            });
        },
        confirmLogins() {
            let user = this.loginForm
            const loginInfo = { account: user.username, password: user.password }
            const data = encrypt(JSON.stringify(loginInfo), this.secretKey)
            const base64Data = Base64.encode(data.toString())
            const postData = { bodyStr: data + base64Data } //ras 加密串 + base64加密串
            this.logins(JSON.stringify(postData))
        },
        async logins(value) {
            const resp = await login(value)
            let respData = resp.data
            if (respData && respData.success) {
                this.loading = false
                let data = respData.data
                data.id = data.userPrimaryKey
                this.$store.commit('setUser', data)
                this.$router.push('/workspace')
            } else {
                //  this.$message('登录失败！错误：'+resp.message);
                ElMessage.error('登录失败！错误：' + respData.message)
                this.loading = false
                this.getCaptcha()
            }
        },
        async getCaptcha() {
            try {
                // 获取二进制的图片数据流
                let resp = await getCaptcha()
                this.captchaImage = 'data:image/jpg;base64,' + arrayBufferToBase64(resp.data);

            } catch (error) {
                console.error('Failed to load captcha image', error)
            }
        }
    },
    created() {
        getAuthCodeKey().then(resp => {
            if (resp.data && resp.data.success) {
                this.secretKey = resp.data.key
                this.$store.commit('setSecretKey', this.secretKey)
                // this.getCaptcha()
            } else {
                ElMessage.error('登录失败！错误：' + resp.message)
            }
        })
    }
};
</script>

<style lang="less">
.login-page {
    width: 100vw;
    height: 100vh;
    background-image: url("@/assets/image/login/background.jpg");
    background-size: 100% 100%;
    .wrap {
        position: absolute;
        right: 12vw;
    }
    .code-wrap {
        display: flex;
        img {
            width: 128px;
            height: 32px;
            margin-left: 14px;
        }
    }
    .el-form-item {
        margin-bottom: 32px;
        .el-form-item__label {
            line-height: 32px;
            font-size: 14px;
        }
    }
    .el-input__wrapper {
        background-color: #F8F8F8;
        border: none;
    }
    .el-input .el-input__inner {
        background-color: #F8F8F8;
        border: none;
        height: 32px;
        line-height: 32px;
    }
    .loginContainer {
        border-radius: 8px;
        background-clip: padding-box;
        margin: 20px auto;
        width: 380px;
        padding: 15px 35px 35px 35px;
        background: #fff;
        .el-button {
            height: 32px;
        } //  box-shadow: 0 0 15px #69a5e0;
    }
    .loginTitle {
        text-align: center;
        font-size: 30px;
        color: #fff;
        margin-top: 25vh;
        margin-bottom: 30px;
    }
    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
}
</style>